/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.jctc.els.modules.biz.service.config.impl;
import java.time.LocalDateTime;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jctc.els.common.constant.ConfigConstant;
import org.jctc.els.modules.biz.entity.Config;
import org.jctc.els.modules.biz.mapper.ConfigMapper;
import org.jctc.els.modules.biz.service.config.IConfigService;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;


/**
 *  服务实现类
 *
 * @author Blade
 * @since 2022-01-14
 */
@Service
public class ConfigServiceImpl implements IConfigService {

	@Autowired
	ConfigMapper configMapper;

	@Override
	public IPage<Config> selectConfigPage() {
		IPage<Config> page = new Page<>();
		return page.setRecords(configMapper.selectConfigPage(page));
	}

	@Override
	public Boolean update(Config config) {
		return configMapper.updateById(config)>0;
	}

	@Override
	public Config selectByName(String name) {
		Config config = configMapper.selectOne(new QueryWrapper<Config>().lambda().eq(Config::getName, name));

		if(Func.isEmpty(config)){
			config = new Config();
			config.setName(ConfigConstant.KEY_ALI_DISTRICTS_VERSION);
			config.setValuej("");
			config.setRemark("阿里省市区的版本号（获取省市区数据MD5之后的值）");
			config.setUpdatetime(LocalDateTime.now());
			configMapper.insert(config);
		}
		return config;
	}
}
